package com.wk.service.implement;

import com.wk.dao.Userinfordao;
import com.wk.pojo.*;
import com.wk.pojo.Class;
import com.wk.service.Userinfoservice;
import com.wk.utils.stringutils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.util.List;

/**
 * @program: thesis
 * @description: User信息的实现类
 * @author: 王侃
 * @create: 2020-02-13 17:13
 **/
@Service
public class Userinforimple implements Userinfoservice {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Autowired
    Userinfordao userinfordao;

    @Override
//    @Caching(
//            cacheable = {@Cacheable(key = "#username")},
//            put = {@CachePut(key = "#result.id")}
//    )
    public User selectbyusername(String username) {
        return userinfordao.selectbyusername(username);
    }

    @Override
    public void insertintouser(User user) {
        userinfordao.insertintouser(user);
    }

    @Override
    public void insertintoimages(User_image_dto dto) {
        //修改文件名称
        String filname_img = stringutils.refilename(dto.getI_filename());
        //存入信息
        String filepath_img = dto.getI_uploadpath() + "/" + filname_img;

        try {
            //输入流
            StreamUtils.copy(dto.getI_inputStream(), new FileOutputStream(filepath_img));
        } catch (IOException e) {
//            throw new FileUploadException("文件上传失败");
        }

        //dto数据存入数据库
        User_image image=new User_image();
        image.setUser_id(dto.getUser_id());
        image.setPath(filepath_img);
        userinfordao.insertintoimages(image);
    }

    @Override
    public void updatemyicon(User_image_dto dto) {
        //修改文件名称
        String filname_img = stringutils.refilename(dto.getI_filename());
        //存入信息
        String filepath_img = dto.getI_uploadpath() + "/" + filname_img;

        try {
            //输入流
            StreamUtils.copy(dto.getI_inputStream(), new FileOutputStream(filepath_img));
        } catch (IOException e) {
//            throw new FileUploadException("文件上传失败");
        }

        //dto数据存入数据库
        User_image image=new User_image();
        image.setUser_id(dto.getUser_id());
        image.setPath(filepath_img);

        userinfordao.updatebyuser_icon(image);

    }

    @Override
    //给新注册的教师发送邮件,这里需要开启异步操作
    @Async
    public void sendemail(String email,String username,String password) throws MessagingException {
        MimeMessage mimeMessage=mailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);

        helper.setSubject("注册信息");
        helper.setText("<b>用户名"+username+"</b><br/>密码"+password);

        helper.setTo(email);
        helper.setFrom("1019591719@qq.com");

//        helper.addAttachment("1.jpg",new File("D:\\Google\\back.jpg"));

        mailSender.send(mimeMessage);
    }

    @Override
    public String getimages(Integer user_id) {
        return userinfordao.getimages(user_id);
    }

    @Override
    public void showmedia(String path, OutputStream outputStream) {
        try {
            StreamUtils.copy(new FileInputStream(path), outputStream);
        } catch (Exception e) {
        }
    }

    @Override
    public List<Class> selectbyallclass() {
        return userinfordao.selectbyallclass();
    }

    @Override
    public User selectbyuserid(Integer id) {
        return userinfordao.selectbyuserid(id);
    }

    @Override
    public List<Class> selectbyclass() {
        return userinfordao.selectbyallclass();
    }

    @Override
    public void updatebyuser(User user) {
        userinfordao.updatebyuser(user);
    }

    @Override
    public List<User> selectbyusername_friends(String username) {
        return userinfordao.selectbyusername_friends(username);
    }

    @Override
    public List<Friends> selectbyallmy_friends(Integer my_id) {
        return userinfordao.selectbyallmy_friends(my_id);
    }

    @Override
    public void insertintoFriends(Friends friends) {
        userinfordao.insertintoFriends(friends);
    }

    @Override
    public Friends selectbyme_his_friend(Integer my_id, Integer his_id) {
        return userinfordao.selectbyme_his_friend(my_id, his_id);
    }
}
